$(function(){
	var promotionPlanNameStr="";
	var promotionUnitNameStr=""; 
	var keywordStr="";
	var clickRateStr="";
	$.post('/material/showMaterial',function(result){
		
		promotionPlanNameStr = result.result.promotionPlanName;
		
		promotionUnitNameStr = result.result.promotionUnitName;

		keywordStr = result.result.keyword;
		clickRateStr = result.result.clickRate;
	var promotionPlanName = promotionPlanNameStr.split(",");
	var promotionUnitName = promotionUnitNameStr.split(",");
	var keyword = keywordStr.split(",");
	var clickRate = clickRateStr.split(",");
	var star = "";
	for(i=0;i<promotionUnitName.length;i++){
			
		
		if(clickRate[i]=="null"||clickRate[i]==0){
			star = "<td><img src=\"../img/star.png\" width=\"30px\" height=\"30px\"></td>";
		}else if(0<clickRate[i]<0.01){
			star = "<td><img src=\"../img/star.png\" width=\"30px\" height=\"30px\"><img src=\"../img/star.png\" width=\"30px\" height=\"30px\"></td>";
		}else{
			star = "<td><img src=\"../img/star.png\" width=\"30px\" height=\"30px\"><img src=\"../img/star.png\" width=\"30px\" height=\"30px\"><img src=\"../img/star.png\" width=\"30px\" height=\"30px\"></td>";
		}
		
		$('#materialTable').append("<tr class=\"group_list\" id=\""+i+"\">"	
				
		+"<td><input type=\"checkbox\" name='check' class=\"js-select-tr\"><label>"+promotionPlanName[i]+"</label></td>"
		+"<td>"+promotionUnitName[i]+"</td>"
		+"<td>"+keyword[i]+"</td>"				
		+star
		+"<td class=\"js-price\" id=\"price\">0.3</td></tr>"
		);
		
	}
	})	
	//原有计划
	$(".js-referrer").focus(function(){
		$(this).parents(".select-campaign").find(".js-uploadkw-radio").attr("checked",true);
		$(".create-newcampaign").find(".js-uploadkw-radio").attr("checked",false);
	})
	// 新建计划
	$(".js-new-build").focus(function(){
		$(this).parents(".unit-group").find(".js-uploadkw-radio").attr("checked",false);
		$(this).parent().find(".js-uploadkw-radio").attr("checked",true);
	})
	// 下拉
	$(".js-ui-icon").click(function(){
		$(".js-referrer").hide();
		$(".js-select").show();
	})

	// 选择出价
	$(".js-radio-box-all").click(function(){
		$(this).attr("checked",true);
		$(this).parent().find(".js-unit-bid").attr("checked",false);
	})
	$(".js-unit-bid").click(function(){
		$(this).attr("checked",true);
		$(this).parent().find(".js-radio-box-all").attr("checked",false);
	})

	$(".js-select-tr").click(function(){
		$(this).attr("checked");
		if ($(this).attr("checked") == "checked") {
			$(this).attr("checked",true);
		} else {
			$(this).attr("checked",false);
		}
	})


	// 应用
	$(".js-use").click(function(){
		var dollarText = $(this).parent().find(".js-input-dollar").val();
		var boxAll = $(".js-radio-box-all").attr("checked");
		var nuitBid = $(".js-unit-bid").attr("checked");
		if (dollarText == "") {
			alert("金额设置不能为空");
		}if(dollarText<0.3){
			alert("金额设置不能低于0.3");
		}else {
		
			if (boxAll == "checked") {
				$(".js-price").text(dollarText);
			}
			if(nuitBid == "checked") {
		        $("input[name='check']:checked").each(function() { // 遍历选中的checkbox
		            n = $(this).parents("tr").index();  // 获取checkbox所在行的顺序
		            $(".tree").find("tr:eq("+n+")").find(".js-price").text(dollarText);
		        });
			}
		}
	})

	// 全选
	$(".js-check-all").click(function(){
		$(".js-select-tr").attr("checked",true);
	})
	// 反选
	$(".js-versa-check").click(function(){
         for(i=0;i<$(".js-select-tr").length;i++){
             if($(".js-select-tr")[i].checked==true){
                $(".js-select-tr")[i].checked=false;
             } else{
             	$(".js-select-tr")[i].checked=true
             }
             
         };
     });
	$(".js-uploading-btn").click(function(){
		
		  
        	obj = document.getElementsByName("check");
		    var ppnstr = "";
		    var punstr = "";
		    var kwstr = "";
		    var crstr = "";
		    var pricestr = "";
		    $("input[name='check']:checked").each(function() { // 遍历选中的checkbox
	            n = $(this).parents("tr").index();  // 获取checkbox所在行的顺序
	            ppnstr = ppnstr + $(".tree").find("tr:eq("+n+")").find("td:eq("+0+")").text()+",";
	            punstr = punstr + $(".tree").find("tr:eq("+n+")").find("td:eq("+1+")").text()+",";
	            kwstr = kwstr + $(".tree").find("tr:eq("+n+")").find("td:eq("+2+")").text()+",";
	            crstr = crstr + $(".tree").find("tr:eq("+n+")").find("td:eq("+3+")").text()+",";	           
	            pricestr = pricestr + $(".tree").find("tr:eq("+n+")").find(".js-price").text()+",";
	        });
		   /*  for(k in obj){
		        if(obj[k].checked){
		        	
		        	ppnstr = ppnstr + promotionPlanName[k]+",";
		        	punstr = punstr + promotionUnitName[k]+",";
		        	kwstr = kwstr + keyword[k]+",";
		        	crstr = crstr + clickRate[k]+",";        	
		        }
		    } */
		    if(ppnstr==null||ppnstr==""){
		    	alert("请选择要上传的关键词");
		    	return;
		    }
		    $(".fied").show();
		    ppnstr = ppnstr.substring(0,ppnstr.length-1);
		    punstr = punstr.substring(0,punstr.length-1);
		    kwstr = kwstr.substring(0,kwstr.length-1);
		    crstr = crstr.substring(0,crstr.length-1);
		    pricestr = pricestr.substring(0,pricestr.length-1); 
		    $.get("/material/uploadMaterial",
		    {		    			    	
		    	ppnstr:ppnstr,
		    	punstr:punstr,
		    	kwstr:kwstr,
		    	crstr:crstr,
		    	pricestr:pricestr
		    	
		    },
		    function(result){
		    	var r = result.result;
		    	$(".fied").hide();
		    	window.location.href= encodeURI("accomplish_upload.html?r="+r);
		    }); 
		   
	});
	
	
})
function GetQueryString(name)
{
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = decodeURI(window.location.search).substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
}
function setIfOpen(val) {
    document.cookie = "ifOpen=" + val;
  }

  function getIfOpenVal(param) {
    var strCookie = document.cookie;
    var arrCookie = strCookie.split(";");
    for (var i = 0; i < arrCookie.length; i++) {
      var arr = arrCookie[i].split("=");
      if (arr[0] == param)
        return arr[1];
    }
    return null;
  }
//收缩服务
var ifOpen = getIfOpenVal("ifOpen");


// index页面默认展开浮窗
if(ifOpen == 0){
  $(".business-bridge-iframe").animate({
    left : "-230px",
  }, 0)
  $(".js-shrink-button").find("span").removeClass("control").text("服务指导");
  
  $(".js-shrink-button").toggle(function() {
    $(".business-bridge-iframe").animate({
      left : "0px",
    }, 1000)
    $(this).find("span").addClass("control").text("收起");
    setIfOpen(1);
  }, function() {

    $(".business-bridge-iframe").animate({
      left : "-230px",
    }, 1000)
    $(this).find("span").removeClass("control").text("服务指导");
    setIfOpen(0);
  })
}else{

  $(".js-shrink-button").toggle(function() {
    $(".business-bridge-iframe").animate({
      left : "-230px",
    }, 1000)
    $(this).find("span").removeClass("control").text("服务指导");
    setIfOpen(0);
  }, function() {

    $(".business-bridge-iframe").animate({
      left : "0px",
    }, 1000)
    $(this).find("span").addClass("control").text("收起");
    setIfOpen(1);
  })

}